<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>简易猜数字游戏</h2>
    <p>请输入一个1到100之间的数字:</p>
    <input type="number" id="guessInput" min="1" max="100">
    <button onclick="checkGuess()">猜</button>
    <p id="result"></p>

    <script>
        let secretNumber = Math.floor(Math.random() * 100) + 1;
        let attempts = 0;

        function checkGuess() {
            const userGuess = parseInt(document.getElementById('guessInput').value);
            const resultParagraph = document.getElementById('result');
            attempts++;

            if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
                resultParagraph.textContent = '请输入一个1到100之间的有效数字';
                return;
            }

            if (userGuess === secretNumber) {
                resultParagraph.textContent = `恭喜你，猜对了！你总共猜了 ${attempts} 次。`;
            } else if (userGuess < secretNumber) {
                resultParagraph.textContent = '猜小了！再试一次。';
            } else {
                resultParagraph.textContent = '猜大了！再试一次。';
            }
        }
    </script>    
</body>
</html>